package aViewLayer;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.InputStream;

import javax.imageio.ImageIO;

import aControllerLayer.PauseMenuController;
import aModelLayer.BattleCharacter;
import aModelLayer.Game;
import aModelLayer.Menu;
import aaGame.RpgGamePanel;

public class PauseView extends View {
	Game mModel;
	PauseMenuController controller;
	
	BattleCharacter[] battleCharacters;
	
	Menu mainMenu;
	
	private Font font;
	private FontMetrics fm;
	
	public Image menuArrow;
	
	private Menu currentMenu;
	
	public PauseView(Game model, PauseMenuController controller) {
		mModel = model;
		this.controller = controller;
		mainMenu = controller.getMainMenu();
		
		battleCharacters = model.getBattleCharacters();
		
		currentMenu = mainMenu;
		
		try {
			InputStream is = this.getClass().getResourceAsStream(
					"/fonts/PetMe128.ttf");
			font = Font.createFont(Font.TRUETYPE_FONT, is);
			font = font.deriveFont(24.0f);

			menuArrow = ImageIO.read(new File(this.getClass().getResource("/images/menuArrow2.png").toURI()));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void render(Graphics2D g, RpgGamePanel root) {
		
		g.setColor(Color.WHITE);
		g.setStroke(new BasicStroke(20));

		g.drawRoundRect(8, 8, (2 * root.getWidth() / 3) - 18,
				root.getHeight() - 18, 8, 8);
		g.drawRoundRect((2 * root.getWidth() / 3) + 12, 8,
				(root.getWidth() / 3) - 20, root.getHeight() - 18, 8, 8);

		GradientPaint gradient = new GradientPaint(0, 0, Color.BLUE,
				root.getWidth(), root.getHeight(), Color.BLACK, true);
		g.setPaint(gradient);
		g.fillRoundRect(5, 5, (2 * root.getWidth() / 3) - 10,
				root.getHeight() - 10, 15, 15);
		g.fillRoundRect((2 * root.getWidth() / 3) + 8, 5,
				(root.getWidth() / 3) - 12, root.getHeight() - 10, 15, 15);

		g.setPaint(null);
		g.setColor(Color.WHITE);
		g.setFont(font);
		fm = g.getFontMetrics();
		int lineHeight = fm.getHeight() + 35;
		int yOffset = 50;
		for (int i = 0; i < currentMenu.getOptions().size(); i++) {
			drawString(g, currentMenu.getOptions().get(i), (2 * root.getWidth() / 3) + 45,
					yOffset, root.getWidth() / 3);
			if (currentMenu.getOptions().get(i).equals(currentMenu.getCurrentOption())) {
				g.drawImage(menuArrow, (2 * root.getWidth() / 3) + 25,
						yOffset - (lineHeight - 35),
						(2 * root.getWidth() / 3) + 40, yOffset, 0, 0,
						menuArrow.getWidth(null),
						menuArrow.getHeight(null), null);
			}

			yOffset += lineHeight + 15;
		}

		
	}
	
	public void drawString(Graphics2D g, String s, int x, int y, int width) {
		FontMetrics fm = g.getFontMetrics();

		int lineHeight = fm.getHeight();

		int xOffset = x;
		int yOffset = y;

		String[] words = s.split(" ");

		for (String word : words) {
			int wordWidth = fm.stringWidth(word + " ");

			if (xOffset + wordWidth >= x + width) {
				yOffset += lineHeight;
				xOffset = x;
			}

			g.drawString(word, xOffset, yOffset);

			xOffset += wordWidth;
		}
	}
}
